<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>更新角色</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="${resRoot}/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="${resRoot}/layui/admin/admin.css" media="all" />
	<link id="layuicss-layer" rel="stylesheet" href="${resRoot}/layui/css/modules/layer/default/layer.css" media="all">
</head>
<body class="childrenBody">
<div class="layui-fluid">   
          <div class="layui-card">
          <div class="layui-card-body">
	<!-- <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
	  <legend>系统角色管理-<#if roleDTO??>更新<#else>新增</#if></legend>
	</fieldset> -->
	<form class="layui-form layui-form-pane" action="#" method="post" style="margin-top: 20px;" >
		<input type="hidden" id="id" name="id" value="<#if roleDTO??>${roleDTO.id}<#else>0</#if>" >
		<div class="layui-form-item">
		    <label class="layui-form-label">角色名称</label>
		    <div class="layui-input-block">
		      <input type="text" name="roleName" id="roleName" lay-verify="required" autocomplete="off" value="<#if roleDTO??>${roleDTO.roleName!}</#if>" placeholder="请输入角色名称" class="layui-input">
		    </div>
  		</div>
  		<div class="layui-form-item">
		    <label class="layui-form-label">角色编码</label>
		    <div class="layui-input-block">
		      <input type="text" name="roleCode" id="roleCode" lay-verify="required" autocomplete="off" value="<#if roleDTO??>${roleDTO.roleCode!}</#if>" placeholder="请输入角色编码" class="layui-input">
		    </div>
  		</div>
  		<div class="layui-form-item">
		    <label class="layui-form-label">备注</label>
		    <div class="layui-input-block">
		      <input type="text" name="remark" id="remark" lay-verify="" autocomplete="off" value="<#if roleDTO??>${roleDTO.remark!}</#if>" placeholder="请输入备注" class="layui-input">
		    </div>
  		</div>
  		<div class="layui-form-item">
		    <label class="layui-form-label">角色授权</label>
		    <div class="layui-input-block">
			    <div id="menuTree" class="ztree" >
			    </div>
			    <input id="menuIds" name="menuIds" type="hidden" value="${roleMenuIds!}">
		    </div>
  		</div>
  		
  		<div class="layui-form-item">
		    <div class="layui-input-block">
		      <button type="button" id="submitBtn" class="layui-btn" lay-submit="" lay-filter="demo1" >立即提交</button>
		      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
		      <button type="button" class="layui-btn layui-btn-primary" onclick="backList();" >返回</button>
		    </div>
	  	</div>
	</form>
	</div>
	  	</div>
	  	</div>
	<script type="text/javascript" src="${resRoot}/layui/layui.js"></script>
	<script type="text/javascript" src="${resRoot}/js/jquery.min.js"></script>
<link href="${resRoot}/jquery-ztree/3.5.40/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" type="text/css"/>
<script src="${resRoot}/jquery-ztree/3.5.40/js/jquery.ztree.all.min.js" type="text/javascript"></script>
	<script type="text/javascript">
	function backList() {
		layer.closeAll("iframe");
	    //刷新父页面
	    parent.location.reload();
	}
	layui.use('form', function(){

		var setting = {check:{enable:true,nocheckInherit:true},view:{selectedMulti:false},
				data:{simpleData:{enable:true}},callback:{beforeClick:function(id, node){
					tree.checkNode(node, !node.checked, true, true);
					return false;
				}}};

		var treeJson;

		  $.ajax({
				type : "post",
				url : "${base}/sys/role/getMenuZTree",
				data : {},
				dataType : "json",
				async : false,
				success : function(data) {
					treeJson= data.message;
				}
			}); 
		// 用户-菜单
		var zNodes=treeJson;
		// 初始化树结构
		var tree = $.fn.zTree.init($("#menuTree"), setting, zNodes);
		// 不选择父节点
		tree.setting.check.chkboxType = { "Y" : "ps", "N" : "s" };
		// 默认选择节点
		var ids = "${roleMenuIds}".split(",");
		for(var i=0; i<ids.length; i++) {
			var node = tree.getNodeByParam("id", ids[i]);
			try{tree.checkNode(node, true, false);}catch(e){}
		}
		// 默认展开全部节点
		tree.expandAll(true);

	$("#submitBtn").click(function(){
			var ids = [], nodes = tree.getCheckedNodes(true);
			for(var i=0; i<nodes.length; i++) {
				ids.push(nodes[i].id);
			}
			$("#menuIds").val(ids);
			var menuIds=$("#menuIds").val();
			// layer.msg(menuIds);
			var id=$("#id").val();
			var roleName=$("#roleName").val();
			var remark=$("#remark").val();
			var roleCode=$("#roleCode").val();
			
			if(checkRoleName()== true) {
				$.ajax({
					type : "post",
					url : "${base}/sys/role/save",
					data : {
						id : id,
						roleName : roleName,
						menuIds : menuIds,
						remark : remark,
						roleCode : roleCode
						},
					dataType : "json",
					async : false,
					success : function(data) {
						if(data.message == 2) {
								layer.msg('角色名称已存在！');   
								// return false;
							} else if(data.message == 3) {
								layer.msg('角色名称不能为空！'); 
							}  else if(data.message == 4) {
								layer.msg('角色编码已存在！'); 
							} else {
								layer.msg('保存成功！', {
									  icon: 1,
									  time: 2000 //2秒关闭（如果不配置，默认是3秒）
									}, function(){
										backList();
									});   
								}
					}
				}); 
			}
		});

	});

	function checkRoleName() {
		var id=$("#id").val();
		var roleName = $("#roleName").val()
		var flag = false;
		if(roleName == "" || roleName==null || roleName =="") {
			layer.msg('角色名称不能为空！');  
			flag = false;
			} else {
				$.ajax({
					type : "post",
					url : "${base}/sys/role/checkRoleName",
					data : {
						id : id,
						roleName : roleName
						},
					dataType : "json",
					async : false,
					success : function(data) {
						if(data.message == 2) {
								layer.msg('角色名称已存在！');   
								flag = false;
							} else {
								flag = true;
							}
					}
				}); 
			}
		return flag;
		}

	</script>
</body>
</html>